﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using System;

public class Guide : MonoBehaviour {

    private Transform currentPoint;
    private Transform startGo;
    private Transform endGo;
    private Transform selfGo;
    public SelectingState SelectingState { get; private set; }
    public Action<string> warningAction;
    public Vector3 SelfPos { get { return selfGo.position; } }

    private void Awake()
    {
        SelectingState = SelectingState.None;
        startGo = transform.Find("Start");
        endGo = transform.Find("End");
        selfGo = transform.Find("Oneself");
        HideGuidePoint();
    }

    public void CancelSelect()
    {
        SelectingState = SelectingState.None;
    }

    public bool StartGuide()
    {
        if (IsAllSelected())
        {
            GetComponent<CustomDirectionsFactory>().SpawnGuidePath();
            return true;
        }
        else
        {
            return false;
        }
        
    }

    public void EndGuide()
    {
        GetComponent<CustomDirectionsFactory>().DestroyPath();
        HideGuidePoint();
    }

    private bool IsAllSelected()
    {
        if (startGo.gameObject.activeSelf == true && startGo.gameObject.activeSelf == true)
        {
            return true;
        }
        else
        {
            if(warningAction != null)
            {
                warningAction("起点或终点未选取");
            }
            return false;
        }
    }

    public void SetPoint(Vector3 clickPos)
    {
        Transform temp = null;
        switch (SelectingState)
        {
            case SelectingState.Start:
                temp = startGo;
                startGo.gameObject.SetActive(true);
                break;
            case SelectingState.End:
                temp = endGo;
                endGo.gameObject.SetActive(true);
                break;
        }
        SetPointPos(temp, clickPos);
        SelectingState = SelectingState.None;
    }

    private void SetPointPos(Transform point,Vector3 pos)
    {
        if (point == null)
            return;
        point.position = new Vector3(pos.x, point.position.y, pos.z);
    }

    public void SetSelfPoint(Vector3 pos)
    {
        selfGo.position = new Vector3(pos.x, selfGo.position.y, pos.z);
    }

    public void SelectMapPoint(Point point)
    {
        switch (point)
        {
            case Point.Start:
                SelectingState = SelectingState.Start;
                break;
            case Point.End:
                SelectingState = SelectingState.End;
                break;
            default:
                SelectingState = SelectingState.None;
                break;
        }
    }

    public void SelectSelfPoint(Point point)
    {
        switch (point)
        {
            case Point.Start:
                startGo.position = selfGo.position;
                startGo.gameObject.SetActive(true);
                break;
            case Point.End:
                endGo.position = selfGo.position;
                endGo.gameObject.SetActive(true);
                break;
        }
    }

    private void HideGuidePoint()
    {
        startGo.gameObject.SetActive(false);
        endGo.gameObject.SetActive(false);
    }
}

public enum Point
{
    Start,
    End
}

public enum SelectingState
{
    None,
    Start,
    End
}
